var app = getApp();
Page({
  data: {
    hotel: {},
    currentSwiper: 0,
    imgheights: [],
    circular: true,
    imgwidth: 750,
    current: 0,
    checkInDate: '',
    checkOutDate: '',
    checkInDateText: '',
    checkOutDateText: '',
    diffDays: '',
    scrollTop: 0,
    toView: '',
    roomList: [],
    page: 1,
    pagesize: 10,
    hasMoreData: true
  },
  imageLoad: function (e) {
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;
    //console.log(imgwidth, imgheight)
    //计算的高度值
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights = this.data.imgheights
    //把每一张图片的高度记录到数组里
    imgheights.push(imgheight)
    this.setData({
      imgheights: imgheights,
    })
  },
  gotoDataRoom() {
    this.setData({
      toView: 'dataRoom'
    })
  },
  onLoad: function (options) {
    let self = this
    wx.setStorage({
      key: 'hotel_id',
      data: options.id,
    })
    wx.getSystemInfo({
      success: function (res) {
        self.setData({
          clientHeight: res.windowHeight
        });
      }
    });
    this.loadDetail(options.id)
  },
  onShow: function (options) {
    this.setDefaultTime()
    
    this.setData({
      page: 1
    })
    this.loadRoom()
  },
  loadDetail(id) {
    var self = this
    app.request({
      url: '/home/hotel/detail',
      method: 'POST',
      data: {
        id: id
      },
      success: (res) => {
        self.setData({
          hotel: res.result
        })
      }
    })
  },
  callPhone(e) {
    var phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
      success: () => {
      }
    })
  },
  openWechatMap(e){
    let self = this
    wx.getSetting({
      success: (res) => {
        var auth = res.authSetting['scope.userLocation']
        console.log(auth)
        if (auth != undefined && auth != true) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
            success: function (resc) {
              if (resc.confirm) {
                wx.openSetting({
                  success:function(data){
                    console.log(data)
                    if (data.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 5000
                      })
                      //再次授权，调用getLocationt的API
                      self.showMap(self)
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 5000
                      })
                    }
                  }
                })
              } else {
                console.log("拒绝授权")
              }
            }
          })
        }else{
          self.showMap(self)
        }
      },
      fail:(res)=>{
        console.log("getSettings Fail:" + res)
      }
    })
  },
  showMap(self){
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        wx.openLocation({
          latitude: Number(self.data.hotel.map_x),
          longitude: Number(self.data.hotel.map_y),
          name: self.data.hotel.hotel_name,
          address: self.data.hotel.address,
          fail: (res) => {
            console.log("wx.openLocation Faild")
            console.log(res)
          }
        })
      },
      fail:(res)=>{
        console.log("wx.getLocation Faild")
        console.log(res)
      }
    })
  },
  loadRoom() {
    var self = this
    wx.getStorage({
      key: 'hotel_id',
      success: function (res) {
        console.log(res)
        var id = res.data
        app.request({
          url: '/home/room/getList',
          method: 'POST',
          data: {
            hotel_id: id,
            checkin_time: self.data.checkInDate,
            checkout_time: self.data.checkOutDate,
            page: self.data.page,
            pagesize: self.data.pagesize
          },
          success: (res) => {
            var listTemp = self.data.roomList
            if (self.data.page == 1) {
              listTemp = []
              self.setData({
                dataList: []
              })
            }
            if (res.result != null && res.result.length > 0) {
              var list = res.result
              if (list.length < self.data.pagesize) {
                self.setData({
                  roomList: listTemp.concat(list),
                  hasMoreData: false
                })
              } else {
                self.setData({
                  roomList: listTemp.concat(list),
                  hasMoreData: true,
                  page: self.data.page + 1
                })
              }
            }
          }
        })
      },
    })
  },

  //下拉
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.loadRoom()
    wx.stopPullDownRefresh();
  },
  //上拉
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.loadRoom()
    } else {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
    }
  },
  setDefaultTime(){
    //今天的时间
    var today = new Date();
    var todayDate = today.getFullYear() + "-" + ((today.getMonth() + 1) < 10 ? "0" + (today.getMonth() + 1) : (today.getMonth() + 1)) + "-" + (today.getDate() < 10 ? "0" + today.getDate() : today.getDate());
    //明天的时间
    var tomorrow = new Date();
    tomorrow.setTime(tomorrow.getTime() + 24 * 60 * 60 * 1000);
    var tomorrowDate = tomorrow.getFullYear() + "-" + ((tomorrow.getMonth() + 1) < 10 ? "0" + (tomorrow.getMonth() + 1) : (tomorrow.getMonth() + 1)) + "-" + (tomorrow.getDate() < 10 ? "0" + tomorrow.getDate() : tomorrow.getDate());
    
    let self = this
    wx.getStorage({
      key: 'ROOM_SOURCE_DATE',
      success: function (res) {
        //设置的值已过期
        if (Date.parse(res.data.checkInDate) - Date.parse(todayDate) < 0){
          wx.setStorage({
            key: 'ROOM_SOURCE_DATE',
            data: {
              checkInDate: todayDate,
              checkOutDate: tomorrowDate
            }
          });
          self.setData({
            checkInDate: todayDate,
            checkOutDate: tomorrowDate,
            checkInDateText: self.getDateText(todayDate),
            checkOutDateText: self.getDateText(tomorrowDate),
            diffDays: self.getDiffDays(todayDate, tomorrowDate)
          })
        }else{
          self.setData({
            checkInDate: res.data.checkInDate,
            checkOutDate: res.data.checkOutDate,
            checkInDateText: self.getDateText(res.data.checkInDate),
            checkOutDateText: self.getDateText(res.data.checkOutDate),
            diffDays: self.getDiffDays(res.data.checkInDate, res.data.checkOutDate)
          })
        }
      },
      fail:function(res){
        wx.setStorage({
          key: 'ROOM_SOURCE_DATE',
          data: {
            checkInDate: todayDate,
            checkOutDate: tomorrowDate
          }
        });
        self.setData({
          checkInDate: todayDate,
          checkOutDate: tomorrowDate,
          checkInDateText: self.getDateText(todayDate),
          checkOutDateText: self.getDateText(tomorrowDate),
          diffDays: self.getDiffDays(todayDate, tomorrowDate)
        })
      }
    })
  },
  getDateText(string) {
    var date = new Date(string);
    return (date.getMonth() + 1) + "月" + date.getDate() + "日"
  },
  getDiffDays(faultDate, completeTime) {
    var stime = Date.parse(new Date(faultDate));
    var etime = Date.parse(new Date(completeTime));
    var usedTime = etime - stime;  //两个时间戳相差的毫秒数
    var days = Math.floor(usedTime / (24 * 3600 * 1000));
    return days;
  }
})